package com.reactnativenavigation.bridge;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import com.easemob.helpdesk.activity.ChangeServiceStatusActivity;
import com.easemob.helpdesk.activity.DispachList;
import com.easemob.helpdesk.activity.HandSignActivity;
import com.easemob.helpdesk.activity.IDCardVerifyStepOneActivity;
import com.easemob.helpdesk.activity.MainActivity;
import com.easemob.helpdesk.activity.MsgSettingActivity;
import com.easemob.helpdesk.activity.MySignContractActivity;
import com.easemob.helpdesk.activity.OrderRewardRulesActivity;
import com.easemob.helpdesk.activity.PreviewPictureActivity;
import com.easemob.helpdesk.activity.ScanQRCodActivity;
import com.easemob.helpdesk.activity.ServiceFeeInstruction;
import com.easemob.helpdesk.utils.DialogUtils;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.housekeeperapp.MainApplication;
import com.reactnativenavigation.controllers.NavigationCommandsHandler;
import com.reactnativenavigation.params.ContextualMenuParams;
import com.reactnativenavigation.params.FabParams;
import com.reactnativenavigation.params.LightBoxParams;
import com.reactnativenavigation.params.SlidingOverlayParams;
import com.reactnativenavigation.params.SnackbarParams;
import com.reactnativenavigation.params.TitleBarButtonParams;
import com.reactnativenavigation.params.TitleBarLeftButtonParams;
import com.reactnativenavigation.params.parsers.ContextualMenuParamsParser;
import com.reactnativenavigation.params.parsers.FabParamsParser;
import com.reactnativenavigation.params.parsers.LightBoxParamsParser;
import com.reactnativenavigation.params.parsers.SlidingOverlayParamsParser;
import com.reactnativenavigation.params.parsers.SnackbarParamsParser;
import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
import com.reactnativenavigation.params.parsers.TitleBarLeftButtonParamsParser;
import com.reactnativenavigation.views.SideMenu.Side;

import java.util.List;

/**
 * The basic abstract components we will expose:
 * BottomTabs (app) - boolean
 * TopBar (per screen)
 * - TitleBar
 * - - RightButtons
 * - - LeftButton
 * - TopTabs (segmented control / view pager tabs)
 * DeviceStatusBar (app) (colors are per screen)
 * AndroidNavigationBar (app) (colors are per screen)
 * SideMenu (app) - boolean, (menu icon is screen-based)
 */
public class NavigationReactModule extends ReactContextBaseJavaModule {
    public static final String NAME = "NavigationReactModule";

    public NavigationReactModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return NAME;
    }

    @ReactMethod
    public void startApp(final ReadableMap params) {
        boolean portraitOnlyMode = false;
        boolean landscapeOnlyMode = false;
        NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void startMainActivityApp(final ReadableMap params) {
        boolean portraitOnlyMode = false;
        boolean landscapeOnlyMode = false;
        NavigationCommandsHandler.startMainActivityApp(params);
    }


    @ReactMethod
    public void startDispatchActivity(final ReadableMap params) {
        if(!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            intent.putExtra("order_id", params.getInt("order_id"));
            intent.putExtra("isdispatch", params.getInt("isdispatch"));

            intent.setClass(activity, DispachList.class);

            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startPreviewPictureActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            intent.putExtra(PreviewPictureActivity.CURRENT_INDEX,params.getInt(PreviewPictureActivity.CURRENT_INDEX));
            intent.putExtra(PreviewPictureActivity.FILE_URIS,params.getString(PreviewPictureActivity.FILE_URIS));
            intent.putExtra(PreviewPictureActivity.IS_URL,params.getBoolean(PreviewPictureActivity.IS_URL));
            intent.setClass(activity,PreviewPictureActivity.class);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startScanQRCodeActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
//            intent.putExtra("")
            intent.setClass(activity, ScanQRCodActivity.class);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startChangeStatusActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            int isRest = params.getInt("isRest");
            Intent intent = new Intent();
            Activity topActivity = MainApplication.getInstance().getTopActivity();
            intent.setClass(topActivity,ChangeServiceStatusActivity.class);
            intent.putExtra("isRest" , isRest);
            topActivity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startIDCardVerifyActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            intent.setClass(activity, IDCardVerifyStepOneActivity.class);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startMySignContractActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            String status = params.getString("status");
            int shopId = params.getInt("shopId");
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            intent.setClass(activity,MySignContractActivity.class);
            intent.putExtra("status",status);
            intent.putExtra("shopId",shopId);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startHandSignActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Activity top = MainApplication.getInstance().getTopActivity();
            Intent intent = new Intent(top, HandSignActivity.class);
            intent.putExtra("ORDER_ID",params.getInt("order_id"));
            top.startActivity(intent);

        }
    }

    @ReactMethod
    public void startServiceFeeExplain(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            Log.d("333333", "startServiceFeeExplain: "+params.getString("selectedDate"));
            intent.putExtra("selectedDate",params.getString("selectedDate"));
            intent.putExtra("phone",params.getString("phone"));
            intent.putExtra("serviceFeeName",params.getString("serviceFeeName"));
            intent.putExtra("groupId",params.getInt("groupId"));
            intent.setClass(activity, ServiceFeeInstruction.class);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startOrderRewardRulesActivity(final ReadableMap params){
        if (!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();
            intent.setClass(activity, OrderRewardRulesActivity.class);
            activity.startActivity(intent);
        }
    }

    @ReactMethod
    public void startMsgSettingActivity(final ReadableMap params) {
        if(!MainApplication.getInstance().isNoActivity()){
            Intent intent = new Intent();
            Activity activity = MainApplication.getInstance().getTopActivity();

            intent.setClass(activity, MsgSettingActivity.class);

            activity.startActivity(intent);
        }
    }


    @ReactMethod
    public void loadingTips(final ReadableMap params) {
        if(params.getString("action").equals("show")){
            if(!MainApplication.getInstance().isNoActivity()){
                Activity activity = MainApplication.getInstance().getTopActivity();
                if(MainApplication.getInstance().loadingDialog != null){
                    MainApplication.getInstance().loadingDialog.dismiss();
                    MainApplication.getInstance().loadingDialog = null;
                }

                if(!activity.isFinishing() && !activity.isDestroyed()){
                    try {
                        MainApplication.getInstance().loadingDialog = DialogUtils.getLoadingDialog(activity, params.getString("tip"));
                        MainApplication.getInstance().loadingDialog.show();
                    }catch(Exception e){
                        MainApplication.getInstance().loadingDialog = null;
                    }

                }else{
                    if(!MainActivity.instance.isDestroyed()){
                        try {
                            MainApplication.getInstance().loadingDialog = DialogUtils.getLoadingDialog(MainActivity.instance, params.getString("tip"));
                            MainApplication.getInstance().loadingDialog.show();
                        }catch(Exception e){
                            MainApplication.getInstance().loadingDialog = null;
                        }

                    }else{
                        if(MainApplication.getInstance().loadingDialog != null){
                            MainApplication.getInstance().loadingDialog.dismiss();
                        }
                    }

                }
            }

        }else{
            if(MainApplication.getInstance().loadingDialog != null){
                MainApplication.getInstance().loadingDialog.hide();
                MainApplication.getInstance().loadingDialog = null;
            }
        }
    }

    @ReactMethod
    public void setScreenTitleBarTitle(String screenInstanceId, String title) {
        NavigationCommandsHandler.setScreenTitleBarTitle(screenInstanceId, title);
    }

    @ReactMethod
    public void setScreenTitleBarSubtitle(String screenInstanceId, String subtitle) {
        NavigationCommandsHandler.setScreenTitleBarSubtitle(screenInstanceId, subtitle);
    }

    @ReactMethod
    public void setScreenButtons(String screenInstanceId, String navigatorEventId,
                                 ReadableArray rightButtonsParams, ReadableMap leftButtonParams, ReadableMap fab) {
        if (rightButtonsParams != null) {
            setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtonsParams);
        }
        if (leftButtonParams != null) {
            setScreenTitleBarLeftButton(screenInstanceId, navigatorEventId, leftButtonParams);
        }
        if (fab != null) {
            setScreenFab(screenInstanceId, navigatorEventId, fab);
        }
    }

    private void setScreenTitleBarRightButtons(final String screenInstanceId, final String navigatorEventId, ReadableArray rightButtonsParams) {
        final List<TitleBarButtonParams> rightButtons = new TitleBarButtonParamsParser()
                .parseButtons(BundleConverter.toBundle(rightButtonsParams));

//        NavigationApplication.instance.runOnMainThread(new Runnable() {
//            @Override
//            public void run() {
//                BaseActivity baseActivity = (BaseActivity) getCurrentActivity();
//                baseActivity.getLayout().setTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtons);
//            }
//        });


        NavigationCommandsHandler.setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtons);
    }

    private void setScreenTitleBarLeftButton(String screenInstanceId, String navigatorEventId, ReadableMap leftButtonParams) {
        TitleBarLeftButtonParams leftButton = new TitleBarLeftButtonParamsParser()
                .parseSingleButton(BundleConverter.toBundle(leftButtonParams));
        NavigationCommandsHandler.setScreenTitleBarLeftButtons(screenInstanceId, navigatorEventId, leftButton);
    }

    private void setScreenFab(String screenInstanceId, String navigatorEventId, ReadableMap fab) {
        FabParams fabParams = new FabParamsParser().parse(BundleConverter.toBundle(fab), navigatorEventId, screenInstanceId);
        NavigationCommandsHandler.setScreenFab(screenInstanceId, navigatorEventId, fabParams);
    }

    @ReactMethod
    public void setBottomTabBadgeByIndex(Integer index, String badge) {
        NavigationCommandsHandler.setBottomTabBadgeByIndex(index, badge);
    }

    @ReactMethod
    public void setBottomTabBadgeByNavigatorId(String navigatorId, String badge) {
        NavigationCommandsHandler.setBottomTabBadgeByNavigatorId(navigatorId, badge);
    }

    @ReactMethod
    public void selectBottomTabByTabIndex(Integer index) {

        NavigationCommandsHandler.selectBottomTabByTabIndex(index);
    }

    @ReactMethod
    public void selectBottomTabByNavigatorId(String navigatorId) {
        NavigationCommandsHandler.selectBottomTabByNavigatorId(navigatorId);
    }

    @ReactMethod
    public void toggleSideMenuVisible(boolean animated, String side) {
        NavigationCommandsHandler.toggleSideMenuVisible(animated, Side.fromString(side));
    }

    @ReactMethod
    public void setSideMenuVisible(boolean animated, boolean visible, String side) {
        NavigationCommandsHandler.setSideMenuVisible(animated, visible, Side.fromString(side));
    }

    @ReactMethod
    public void toggleTopBarVisible(final ReadableMap params) {
    }

    @ReactMethod
    public void setTopBarVisible(String screenInstanceId, boolean hidden, boolean animated) {
        NavigationCommandsHandler.setTopBarVisible(screenInstanceId, hidden, animated);
    }

    @ReactMethod
    public void toggleBottomTabsVisible(final ReadableMap params) {
    }

    @ReactMethod
    public void setBottomTabsVisible(boolean hidden, boolean animated) {
        NavigationCommandsHandler.setBottomTabsVisible(hidden, animated);
    }

    @ReactMethod
    public void push(final ReadableMap params) {
        NavigationCommandsHandler.push(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void pop(final ReadableMap params) {
        NavigationCommandsHandler.pop(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void popToRoot(final ReadableMap params) {
        MainApplication.getInstance().gotoMainActivity();

//        NavigationCommandsHandler.popToRoot(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void newStack(final ReadableMap params) {
        NavigationCommandsHandler.newStack(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void showModal(final ReadableMap params) {
        NavigationCommandsHandler.showModal(BundleConverter.toBundle(params));
    }

    @ReactMethod
    public void showLightBox(final ReadableMap params) {
        LightBoxParams lbp = new LightBoxParamsParser(BundleConverter.toBundle(params)).parse();
        NavigationCommandsHandler.showLightBox(lbp);
    }

    @ReactMethod
    public void dismissLightBox() {
        NavigationCommandsHandler.dismissLightBox();
    }

    @ReactMethod
    public void dismissAllModals() {
        NavigationCommandsHandler.dismissAllModals();
    }

    @ReactMethod
    public void dismissTopModal() {
        NavigationCommandsHandler.dismissTopModal();
    }

    @ReactMethod
    public void showSlidingOverlay(final ReadableMap params) {
        SlidingOverlayParams slidingOverlayParams = new SlidingOverlayParamsParser().parse(BundleConverter.toBundle(params));
        NavigationCommandsHandler.showSlidingOverlay(slidingOverlayParams);
    }

    @ReactMethod
    public void hideSlidingOverlay(final ReadableMap params) {
        NavigationCommandsHandler.hideSlidingOverlay();
    }

    @ReactMethod
    public void showSnackbar(final ReadableMap params) {
        SnackbarParams snackbarParams = new SnackbarParamsParser().parse(BundleConverter.toBundle(params));
        NavigationCommandsHandler.showSnackbar(snackbarParams);
    }

    @ReactMethod
    public void dismissSnackbar() {
        NavigationCommandsHandler.dismissSnackbar();
    }

    @ReactMethod
    public void showContextualMenu(final String screenInstanceId, final ReadableMap params, final Callback onButtonClicked) {
        ContextualMenuParams contextualMenuParams =
                new ContextualMenuParamsParser().parse(BundleConverter.toBundle(params));
        NavigationCommandsHandler.showContextualMenu(screenInstanceId, contextualMenuParams, onButtonClicked);
    }

    @ReactMethod
    public void dismissContextualMenu(String screenInstanceId) {
        NavigationCommandsHandler.dismissContextualMenu(screenInstanceId);
    }

    @ReactMethod
    public void getOrientation(Promise promise) {
        NavigationCommandsHandler.getOrientation(promise);
    }
}
